
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <title>updateClassComponent · React源码解析</title>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.0">
        
        
        
    
    <link rel="stylesheet" href="../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-theme-api/theme-api.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../../styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    

         
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="update-host-root.html" />
    
    
    <link rel="prev" href="update-functional-component.html" />
    

<script
  async
  src="https://www.googletagmanager.com/gtag/js?id=UA-49483742-5"
></script>
<script>
  window.dataLayer = window.dataLayer || []
  function gtag() {
    dataLayer.push(arguments)
  }
  gtag('js', new Date())

  gtag('config', 'UA-49483742-5')
</script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../">
            
                <a href="../../../">
            
                    
                    首页
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    基础
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../api/react.html">
            
                <a href="../../api/react.html">
            
                    
                    React API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../../api/react-element.html">
            
                <a href="../../api/react-element.html">
            
                    
                    ReactElement
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../../api/react-children.html">
            
                <a href="../../api/react-children.html">
            
                    
                    React Children
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../../api/react-structure.html">
            
                <a href="../../api/react-structure.html">
            
                    
                    React 中的数据结构
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    创建更新
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../update/react-dom-render.html">
            
                <a href="../../update/react-dom-render.html">
            
                    
                    ReactDOM.render
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../../update/set-state.html">
            
                <a href="../../update/set-state.html">
            
                    
                    setState & forceUpdate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../../update/expiration-time.html">
            
                <a href="../../update/expiration-time.html">
            
                    
                    expirationTime 计算
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    任务调度
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../scheduler-global.html">
            
                <a href="../scheduler-global.html">
            
                    
                    全局变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../schedule-work.html">
            
                <a href="../schedule-work.html">
            
                    
                    scheduleWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../scheduler-pkg.html">
            
                <a href="../scheduler-pkg.html">
            
                    
                    react-scheduler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../perform-work.html">
            
                <a href="../perform-work.html">
            
                    
                    performWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../perform-unit-of-work.html">
            
                <a href="../perform-unit-of-work.html">
            
                    
                    performUnitOfWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../render-root.html">
            
                <a href="../render-root.html">
            
                    
                    renderRoot
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.6.1" data-path="../render-root/throw-exception.html">
            
                <a href="../render-root/throw-exception.html">
            
                    
                    throwException
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../begin-work.html">
            
                <a href="../begin-work.html">
            
                    
                    beginWork
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.7.1" data-path="mount-indeterminate-component.html">
            
                <a href="mount-indeterminate-component.html">
            
                    
                    mountIndeterminateComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.2" data-path="mount-lazy-component.html">
            
                <a href="mount-lazy-component.html">
            
                    
                    mountLazyComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.3" data-path="update-functional-component.html">
            
                <a href="update-functional-component.html">
            
                    
                    updateFunctionalComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.7.4" data-path="update-class-component.html">
            
                <a href="update-class-component.html">
            
                    
                    updateClassComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.5" data-path="update-host-root.html">
            
                <a href="update-host-root.html">
            
                    
                    updateHostRoot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.6" data-path="update-host-component.html">
            
                <a href="update-host-component.html">
            
                    
                    updateHostComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.7" data-path="update-host-text.html">
            
                <a href="update-host-text.html">
            
                    
                    updateHostText
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.8" data-path="../../feature/Suspense-component.md">
            
                <span>
            
                    
                    updateSuspenseComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.9" data-path="update-portal-component.html">
            
                <a href="update-portal-component.html">
            
                    
                    updatePortalComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.10" data-path="update-forward-ref.html">
            
                <a href="update-forward-ref.html">
            
                    
                    updateForwardRef
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.11" data-path="update-fragment.html">
            
                <a href="update-fragment.html">
            
                    
                    updateFragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.12" data-path="update-mode.html">
            
                <a href="update-mode.html">
            
                    
                    updateMode
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.13" data-path="update-profiler.html">
            
                <a href="update-profiler.html">
            
                    
                    updateProfiler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.14" data-path="update-context-provider.html">
            
                <a href="update-context-provider.html">
            
                    
                    updateContextProvider
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.15" data-path="update-context-consumer.html">
            
                <a href="update-context-consumer.html">
            
                    
                    updateContextConsumer
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../reconcile-children.html">
            
                <a href="../reconcile-children.html">
            
                    
                    reconcileChildren
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.8.1" data-path="../reconcile-children/single.html">
            
                <a href="../reconcile-children/single.html">
            
                    
                    调和单个节点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8.2" data-path="../reconcile-children/array.html">
            
                <a href="../reconcile-children/array.html">
            
                    
                    调和列表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    commit 阶段
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../../commit-phase/commit-root.html">
            
                <a href="../../commit-phase/commit-root.html">
            
                    
                    commitRoot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../../commit-phase/invokeGuardedCallback.html">
            
                <a href="../../commit-phase/invokeGuardedCallback.html">
            
                    
                    invokeGuardedCallback
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../../commit-phase/commitBeforeMutationLifecycles.html">
            
                <a href="../../commit-phase/commitBeforeMutationLifecycles.html">
            
                    
                    commitBeforeMutationLifecycles
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../../commit-phase/commit-all-host-effects.html">
            
                <a href="../../commit-phase/commit-all-host-effects.html">
            
                    
                    commitAllHostEffects
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.4.1" data-path="../../commit-phase/host-effects/CommitPlacement.html">
            
                <a href="../../commit-phase/host-effects/CommitPlacement.html">
            
                    
                    commitPlacement
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4.2" data-path="../../commit-phase/host-effects/CommitWork.html">
            
                <a href="../../commit-phase/host-effects/CommitWork.html">
            
                    
                    commitWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4.3" data-path="../../commit-phase/host-effects/CommitDeletion.html">
            
                <a href="../../commit-phase/host-effects/CommitDeletion.html">
            
                    
                    commitDeletion
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../../commit-phase/commit-all-lifecycles.html">
            
                <a href="../../commit-phase/commit-all-lifecycles.html">
            
                    
                    commitAllLifecycle
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    功能
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../../features/context.html">
            
                <a href="../../features/context.html">
            
                    
                    context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../../features/hydrate.html">
            
                <a href="../../features/hydrate.html">
            
                    
                    hydrate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../../features/ref.html">
            
                <a href="../../features/ref.html">
            
                    
                    ref
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../../features/event-injection.html">
            
                <a href="../../features/event-injection.html">
            
                    
                    事件系统初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../../features/event-bind.html">
            
                <a href="../../features/event-bind.html">
            
                    
                    事件绑定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../../features/event-dispatch.html">
            
                <a href="../../features/event-dispatch.html">
            
                    
                    事件触发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../../features/event-create-event-object.html">
            
                <a href="../../features/event-create-event-object.html">
            
                    
                    事件对象生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../../features/Suspense-component.html">
            
                <a href="../../features/Suspense-component.html">
            
                    
                    Suspense
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../../features/suspense.html">
            
                <a href="../../features/suspense.html">
            
                    
                    挂起任务的含义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../../features/suspense-lazy.html">
            
                <a href="../../features/suspense-lazy.html">
            
                    
                    lazy 组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    Hooks
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../../hooks/hooks-start.html">
            
                <a href="../../hooks/hooks-start.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../../hooks/hooks-use-state.html">
            
                <a href="../../hooks/hooks-use-state.html">
            
                    
                    useState
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../../hooks/hooks-use-effect.html">
            
                <a href="../../hooks/hooks-use-effect.html">
            
                    
                    useEffect
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../../hooks/hooks-others.html">
            
                <a href="../../hooks/hooks-others.html">
            
                    
                    其他API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../../hooks/hooks-common.html">
            
                <a href="../../hooks/hooks-common.html">
            
                    
                    通用API
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <form
            action="https://buttondown.email/api/emails/embed-subscribe/Jokcy"
            method="post"
            target="popupwindow"
            onsubmit="window.open('https://buttondown.email/Jokcy', 'popupwindow')"
            class="buttondown-form"
            >
            <input type="email" name="email" id="bd-email" placeholder="(your@email.com)">
            <input type="hidden" value="1" name="embed"/>
            <button type="submit">订阅我</button>
        </form>
    </li>
</ul>

                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                 
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../.." >updateClassComponent</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="updateclasscomponent">updateClassComponent</h1><div class="api-method"><div class="api-method-definition"><p>&#x5F00;&#x59CB;&#x662F;&#x5BF9;<code>context</code>&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x56E0;&#x4E3A;<code>ClassComponent</code>&#x53EF;&#x4EE5;&#x6210;&#x4E3A;<code>context provider</code>&#x3002;</p><p><code>current === null</code>&#x53EA;&#x4F1A;&#x51FA;&#x73B0;&#x5728;&#x7B2C;&#x4E00;&#x6B21;&#x6E32;&#x67D3;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x56E0;&#x4E3A;&#x4F1A;&#x5148;&#x521B;&#x5EFA;<code>workInProcess</code>&#xFF0C;&#x5728;&#x6E32;&#x67D3;&#x7ED3;&#x675F;&#x4E4B;&#x540E;&#x624D;&#x4F1A;&#x628A;<code>workInProcess</code>&#x62F7;&#x8D1D;&#x6210;<code>current</code>&#xFF0C;&#x4EE3;&#x8868;&#x7740;&#x7B2C;&#x4E00;&#x6B21;&#x6E32;&#x67D3;&#x7ED3;&#x675F;&#x3002;&#x800C;&#x540E;&#x9762;&#x4E5F;&#x4F1A;&#x51FA;&#x73B0;&#x6839;&#x636E;<code>current === null</code>&#x6765;&#x5224;&#x65AD;&#x662F;&#x5426;&#x9700;&#x8981;&#x8C03;&#x7528;<code>componentDidMount</code>&#x7684;&#x4EE3;&#x7801;</p><p>&#x5728;&#x8FD9;&#x91CC;&#x5982;&#x679C;<code>current === null</code>&#x5C31;&#x884C;&#x8981;&#x8FDB;&#x884C;&#x5B9E;&#x4F8B;&#x7684;&#x6784;&#x5EFA;&#x5DE5;&#x4F5C;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x662F;&#xFF0C;&#x76F4;&#x63A5;<a href="#updateClassInstance"><code>updateClassInstance</code></a></p><p>&#x5982;&#x679C;&#x662F;&#x8FD8;&#x8981;&#x5224;&#x65AD;&#x5B9E;&#x4F8B;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x521B;&#x5EFA;<code>workInProgress.stateNode === null</code>&#xFF0C;&#x5982;&#x679C;&#x662F;&#x7684;&#x8BDD;&#x8981;&#x521B;&#x5EFA;&#x8FD9;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF0C;&#x901A;&#x8FC7;<a href="#constructClassInstance"><code>constructClassInstance</code></a>&#xFF0C;&#x5E76;&#x4E14;&#x6302;&#x8F7D;&#x5B9E;&#x4F8B;<a href="#mountClassInstance"><code>mountClassInstance</code></a></p><p>&#x5982;&#x679C;&#x5DF2;&#x7ECF;&#x6709;<code>current</code>&#x5219;&#x8C03;&#x7528;<a href="#updateclassinstance"><code>updateClassInstance</code></a></p><p>&#x6700;&#x540E;&#x8C03;&#x7528;<a href="#finishclasscomponent"><code>finishClassComponent</code></a></p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updateClassComponent</span>(<span class="hljs-params">
  current: Fiber | null,
  workInProgress: Fiber,
  Component: any,
  nextProps,
  renderExpirationTime: ExpirationTime,
</span>) </span>{
  <span class="hljs-comment">// Push context providers early to prevent context stack mismatches.</span>
  <span class="hljs-comment">// During mounting we don&apos;t know the child context yet as the instance doesn&apos;t exist.</span>
  <span class="hljs-comment">// We will invalidate the child context in finishClassComponent() right after rendering.</span>
  <span class="hljs-keyword">let</span> hasContext
  <span class="hljs-keyword">if</span> (isLegacyContextProvider(Component)) {
    hasContext = <span class="hljs-literal">true</span>
    pushLegacyContextProvider(workInProgress)
  } <span class="hljs-keyword">else</span> {
    hasContext = <span class="hljs-literal">false</span>
  }
  prepareToReadContext(workInProgress, renderExpirationTime)

  <span class="hljs-keyword">const</span> instance = workInProgress.stateNode
  <span class="hljs-keyword">let</span> shouldUpdate
  <span class="hljs-keyword">if</span> (instance === <span class="hljs-literal">null</span>) {
    <span class="hljs-keyword">if</span> (current !== <span class="hljs-literal">null</span>) {
      <span class="hljs-comment">// An class component without an instance only mounts if it suspended</span>
      <span class="hljs-comment">// inside a non- concurrent tree, in an inconsistent state. We want to</span>
      <span class="hljs-comment">// tree it like a new mount, even though an empty version of it already</span>
      <span class="hljs-comment">// committed. Disconnect the alternate pointers.</span>
      current.alternate = <span class="hljs-literal">null</span>
      workInProgress.alternate = <span class="hljs-literal">null</span>
      <span class="hljs-comment">// Since this is conceptually a new fiber, schedule a Placement effect</span>
      workInProgress.effectTag |= Placement
    }
    <span class="hljs-comment">// In the initial pass we might need to construct the instance.</span>
    constructClassInstance(
      workInProgress,
      Component,
      nextProps,
      renderExpirationTime,
    )
    mountClassInstance(
      workInProgress,
      Component,
      nextProps,
      renderExpirationTime,
    )
    shouldUpdate = <span class="hljs-literal">true</span>
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (current === <span class="hljs-literal">null</span>) {
    <span class="hljs-comment">// In a resume, we&apos;ll already have an instance we can reuse.</span>
    shouldUpdate = resumeMountClassInstance(
      workInProgress,
      Component,
      nextProps,
      renderExpirationTime,
    )
  } <span class="hljs-keyword">else</span> {
    shouldUpdate = updateClassInstance(
      current,
      workInProgress,
      Component,
      nextProps,
      renderExpirationTime,
    )
  }
  <span class="hljs-keyword">return</span> finishClassComponent(
    current,
    workInProgress,
    Component,
    shouldUpdate,
    hasContext,
    renderExpirationTime,
  )
}</code></pre>
</div></div></div>



<div class="api-method"><div class="api-method-definition"><h1 id="constructClassInstance">constructClassInstance</h1><p>&#x9996;&#x5148;&#x6839;&#x636E;<code>workInProcess.type</code>&#x83B7;&#x53D6;<code>class</code>&#xFF0C;&#x7136;&#x540E;&#x6784;&#x5EFA;<code>instance</code></p><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">adoptClassInstance</span>(<span class="hljs-params">workInProgress, instance</span>) </span>{
  instance.updater = classComponentUpdater
  workInProgress.stateNode = instance
  <span class="hljs-comment">// The instance needs access to the fiber so that it can schedule updates</span>
  set(instance, workInProgress)
  {
    instance._reactInternalInstance = fakeInternalInstance
  }
}</code></pre>
<p>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7ED9; ClassComponent &#x7684;&#x5B9E;&#x4F8B;&#x6302;&#x8F7D;&#x4E00;&#x4E2A;<code>updater</code>&#x5BF9;&#x8C61;</p><pre><code class="lang-js">{
  enqueueForceUpdate: func,
  enqueueReplaceState: func,
  enqueueSetState: func,
  isMounted: func
}</code></pre>
<p>&#x5F88;&#x719F;&#x6089;&#x5BF9;&#x4E0D;&#x5BF9;&#xFF0C;&#x8FD9;&#x51E0;&#x4E2A;&#x65B9;&#x6CD5;&#x5C31;&#x5BF9;&#x5E94;&#x6211;&#x4EEC;&#x5E73;&#x65F6;&#x8C03;&#x7528;&#x7684;<code>forceUpdate, replaceState, setState</code></p><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">set</span>(<span class="hljs-params">key, value</span>) </span>{
  key._reactInternalFiber = value
}</code></pre>
<p>&#x8FD9;&#x91CC;&#x7ED9; instance &#x7684;<code>_reactInternalFiber</code>&#x6302;&#x8F7D;&#x4E0A;<code>workInProgress</code>&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>this._reactInternalFiber</code>&#x83B7;&#x53D6;&#x8BE5;&#x5B9E;&#x4F8B;&#x5BF9;&#x5E94;&#x7684;<code>Fiber</code>&#x5BF9;&#x8C61;</p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><p>&#x5173;&#x4E8E;<code>Context</code>&#x770B;<a href="../../basic/context.html">&#x8FD9;&#x91CC;</a></p><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">constructClassInstance</span>(<span class="hljs-params">
  workInProgress: Fiber,
  ctor: any,
  props: any,
  renderExpirationTime: ExpirationTime,
</span>): <span class="hljs-title">any</span> </span>{
  <span class="hljs-keyword">const</span> unmaskedContext = getUnmaskedContext(workInProgress, ctor, <span class="hljs-literal">true</span>)
  <span class="hljs-keyword">const</span> contextTypes = ctor.contextTypes
  <span class="hljs-keyword">const</span> isContextConsumer = contextTypes !== <span class="hljs-literal">null</span> &amp;&amp; contextTypes !== <span class="hljs-literal">undefined</span>
  <span class="hljs-keyword">const</span> context = isContextConsumer
    ? getMaskedContext(workInProgress, unmaskedContext)
    : emptyContextObject

  <span class="hljs-keyword">const</span> instance = <span class="hljs-keyword">new</span> ctor(props, context)
  <span class="hljs-keyword">const</span> state = (workInProgress.memoizedState =
    instance.state !== <span class="hljs-literal">null</span> &amp;&amp; instance.state !== <span class="hljs-literal">undefined</span>
      ? instance.state
      : <span class="hljs-literal">null</span>)
  adoptClassInstance(workInProgress, instance)

  <span class="hljs-comment">// &#x5220;&#x4E86;&#x4E00;&#x5806;&#x5173;&#x4E8E;&#x5F00;&#x53D1;&#x65F6;&#x63D0;&#x9192;&#x4E0D;&#x5B89;&#x5168;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x65B9;&#x6CD5;&#x7684;&#x903B;&#x8F91;</span>

  <span class="hljs-keyword">if</span> (isContextConsumer) {
    cacheContext(workInProgress, unmaskedContext, context)
  }

  <span class="hljs-keyword">return</span> instance
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">adoptClassInstance</span>(<span class="hljs-params">workInProgress: Fiber, instance: any</span>): <span class="hljs-title">void</span> </span>{
  instance.updater = classComponentUpdater
  workInProgress.stateNode = instance
  <span class="hljs-comment">// The instance needs access to the fiber so that it can schedule updates</span>
  ReactInstanceMap.set(instance, workInProgress)
}</code></pre>
</div></div></div>



<div class="api-method"><div class="api-method-definition"><h1 id="mountClassInstance">mountClassInstance</h1><p>&#x521D;&#x59CB;&#x5316; props&#x3001;state &#x7B49;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#xFF0C;&#x5982;&#x679C;&#x6709;<code>updateQueue</code>&#x5C31;&#x66F4;&#x65B0;&#x4E4B;&#xFF0C;&#x4E00;&#x822C;&#x6765;&#x8BF4;&#x7B2C;&#x4E00;&#x6B21;&#x6E32;&#x67D3;&#x662F;&#x6CA1;&#x6709;&#x7684;&#x3002;</p><p><code>processUpdateQueue</code>&#x7528;&#x6765;&#x8BA1;&#x7B97;&#x65B0;&#x7684;<code>state</code>&#xFF0C;&#x8BE6;&#x89C1;<a href="../../../docs/depth-analysis/process-update-queue.html">&#x8FD9;&#x91CC;</a></p><p>TODO: updateQueue</p><pre><code class="lang-js"><span class="hljs-keyword">var</span> getDerivedStateFromProps = workInProgress.type.getDerivedStateFromProps
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> getDerivedStateFromProps === <span class="hljs-string">&apos;function&apos;</span>) {
  applyDerivedStateFromProps(workInProgress, getDerivedStateFromProps, props)
  instance.state = workInProgress.memoizedState
}</code></pre>
<p>&#x5982;&#x679C; class &#x6709;&#x58F0;&#x660E;<code>getDerivedStateFromProps</code>&#xFF0C;&#x5C31;&#x8C03;&#x7528;&#x4ED6;&#xFF0C;&#x8C03;&#x7528;&#x8BE5;&#x65B9;&#x6CD5;&#x4E4B;&#x540E;&#x4F1A;&#x6839;&#x636E;&#x7ED3;&#x679C;&#x66F4;&#x65B0;<code>workInProgress.memoizedState</code>&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x91CD;&#x65B0;&#x8D4B;&#x503C;&#x7ED9;<code>instance.state</code></p><p>&#x63A5;&#x4E0B;&#x53BB;&#x5224;&#x65AD;&#x4E00;&#x4E0B;&#x662F;&#x5426;&#x6CA1;&#x6709;&#x65B0;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x65B9;&#x6CD5;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x5E76;&#x4E14;&#x6709;<code>componentWillMount</code>&#x751F;&#x547D;&#x5468;&#x671F;&#x65B9;&#x6CD5;&#xFF0C;&#x5219;&#x8C03;&#x7528;&#x4ED6;&#x3002;</p><p>&#x6700;&#x540E;&#x5224;&#x65AD;&#x662F;&#x5426;&#x6709;<code>componentDidMount</code>&#xFF0C;&#x5982;&#x679C;&#x6709;&#x5C31;&#x4FEE;&#x6539;<code>workInProgress.effectTag |= Update</code>&#xFF0C;&#x56E0;&#x4E3A;<code>componentDidMount</code>&#x8981;&#x5728;&#x771F;&#x6B63;&#x6E32;&#x67D3;&#x8FDB;<code>DOM</code>&#x4E4B;&#x540E;&#x624D;&#x8C03;&#x7528;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;<code>commit</code>&#x4E4B;&#x540E;</p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">mountClassInstance</span>(<span class="hljs-params">
  workInProgress: Fiber,
  ctor: any,
  newProps: any,
  renderExpirationTime: ExpirationTime,
</span>): <span class="hljs-title">void</span> </span>{
  <span class="hljs-keyword">if</span> (__DEV__) {
    checkClassInstance(workInProgress, ctor, newProps)
  }

  <span class="hljs-keyword">const</span> instance = workInProgress.stateNode
  <span class="hljs-keyword">const</span> unmaskedContext = getUnmaskedContext(workInProgress, ctor, <span class="hljs-literal">true</span>)

  instance.props = newProps
  instance.state = workInProgress.memoizedState
  instance.refs = emptyRefsObject
  instance.context = getMaskedContext(workInProgress, unmaskedContext)

  <span class="hljs-keyword">let</span> updateQueue = workInProgress.updateQueue
  <span class="hljs-keyword">if</span> (updateQueue !== <span class="hljs-literal">null</span>) {
    processUpdateQueue(
      workInProgress,
      updateQueue,
      newProps,
      instance,
      renderExpirationTime,
    )
    instance.state = workInProgress.memoizedState
  }

  <span class="hljs-keyword">const</span> getDerivedStateFromProps = ctor.getDerivedStateFromProps
  <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> getDerivedStateFromProps === <span class="hljs-string">&apos;function&apos;</span>) {
    applyDerivedStateFromProps(
      workInProgress,
      ctor,
      getDerivedStateFromProps,
      newProps,
    )
    instance.state = workInProgress.memoizedState
  }

  <span class="hljs-keyword">if</span> (
    <span class="hljs-keyword">typeof</span> ctor.getDerivedStateFromProps !== <span class="hljs-string">&apos;function&apos;</span> &amp;&amp;
    <span class="hljs-keyword">typeof</span> instance.getSnapshotBeforeUpdate !== <span class="hljs-string">&apos;function&apos;</span> &amp;&amp;
    (<span class="hljs-keyword">typeof</span> instance.UNSAFE_componentWillMount === <span class="hljs-string">&apos;function&apos;</span> ||
      <span class="hljs-keyword">typeof</span> instance.componentWillMount === <span class="hljs-string">&apos;function&apos;</span>)
  ) {
    callComponentWillMount(workInProgress, instance)
    <span class="hljs-comment">// If we had additional state updates during this life-cycle, let&apos;s</span>
    <span class="hljs-comment">// process them now.</span>
    updateQueue = workInProgress.updateQueue
    <span class="hljs-keyword">if</span> (updateQueue !== <span class="hljs-literal">null</span>) {
      processUpdateQueue(
        workInProgress,
        updateQueue,
        newProps,
        instance,
        renderExpirationTime,
      )
      instance.state = workInProgress.memoizedState
    }
  }

  <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> instance.componentDidMount === <span class="hljs-string">&apos;function&apos;</span>) {
    workInProgress.effectTag |= Update
  }
}</code></pre>
</div></div></div>



<div class="api-method"><div class="api-method-definition"><h1 id="applyDerivedStateFromProps">applyDerivedStateFromProps</h1><p>&#x8C03;&#x7528;<code>getDerivedStateFromProps</code>&#x83B7;&#x5F97;&#x6700;&#x65B0;&#x7684;<code>state</code>&#x5E76;&#x653E;&#x5230;<code>fiber.memoizedState</code>&#x4E0A;</p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">applyDerivedStateFromProps</span>(<span class="hljs-params">
  workInProgress: Fiber,
  getDerivedStateFromProps: (props: any, state: any</span>) =&gt; <span class="hljs-title">any</span>,
  <span class="hljs-title">nextProps</span>: <span class="hljs-title">any</span>,
) </span>{
  <span class="hljs-keyword">const</span> prevState = workInProgress.memoizedState

  <span class="hljs-keyword">const</span> partialState = getDerivedStateFromProps(nextProps, prevState)

  <span class="hljs-comment">// Merge the partial state and the previous state.</span>
  <span class="hljs-keyword">const</span> memoizedState =
    partialState === <span class="hljs-literal">null</span> || partialState === <span class="hljs-literal">undefined</span>
      ? prevState
      : <span class="hljs-built_in">Object</span>.assign({}, prevState, partialState)
  workInProgress.memoizedState = memoizedState

  <span class="hljs-comment">// Once the update queue is empty, persist the derived state onto the</span>
  <span class="hljs-comment">// base state.</span>
  <span class="hljs-keyword">const</span> updateQueue = workInProgress.updateQueue
  <span class="hljs-keyword">if</span> (updateQueue !== <span class="hljs-literal">null</span> &amp;&amp; updateQueue.expirationTime === NoWork) {
    updateQueue.baseState = memoizedState
  }
}</code></pre>
</div></div></div>



<div class="api-method"><div class="api-method-definition"><h1 id="finishClassComponent">finishClassComponent</h1><p>&#x9996;&#x5148;<code>markRef</code></p><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">markRef</span>(<span class="hljs-params">current: Fiber | null, workInProgress: Fiber</span>) </span>{
  <span class="hljs-keyword">const</span> ref = workInProgress.ref
  <span class="hljs-keyword">if</span> (
    (current === <span class="hljs-literal">null</span> &amp;&amp; ref !== <span class="hljs-literal">null</span>) ||
    (current !== <span class="hljs-literal">null</span> &amp;&amp; current.ref !== ref)
  ) {
    <span class="hljs-comment">// Schedule a Ref effect</span>
    workInProgress.effectTag |= Ref
  }
}</code></pre>
<p>&#x4E5F;&#x5C31;&#x662F;&#x7ED9;<code>effectTag</code>&#x52A0;&#x4E0A;<code>Ref</code>&#x5C5E;&#x6027;</p><p>&#x7136;&#x540E;&#x5982;&#x679C;&#x5373;<code>shouldUpdate</code>&#x4E0D;&#x4E3A;<code>true</code>&#xFF0C;&#x53C8;&#x6CA1;&#x6709;&#x6355;&#x83B7;&#x5230;&#x9519;&#x8BEF;&#xFF0C;&#x90A3;&#x4E48;&#x8BF4;&#x660E;&#x8FD9;&#x4E2A;&#x7EC4;&#x4EF6;&#x5DF2;&#x7ECF;&#x66F4;&#x65B0;&#x5B8C;&#x6210;&#x4E86;&#xFF0C;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;<code>bailoutOnAlreadyFinishedWork</code></p><p>&#x4E4B;&#x540E;&#x8FDB;&#x5165;<code>render</code>&#x9636;&#x6BB5;&#xFF0C;&#x8FD9;&#x4E2A;&#x9636;&#x6BB5;&#x4E3B;&#x8981;&#x662F;&#x83B7;&#x53D6;<code>instance.render</code>&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x4F5C;&#x4E3A;&#x5F53;&#x524D;<code>Fiber</code>&#x7684;<code>children</code>&#xFF0C;&#x5230;&#x8FD9;&#x91CC;&#x5C31;&#x7ED3;&#x675F;&#x4E86;<code>render</code>&#x9636;&#x6BB5;&#x3002;</p><p>&#x518D;&#x7ED9;<code>effectTag</code>&#x52A0;&#x4E0A;<code>PerformedWork</code>&#xFF0C;&#x63A5;&#x4E0B;&#x53BB;&#x5C31;&#x8FDB;&#x5165;&#x4E86;&#x65E5;&#x5E38;&#x7684;<code>reconcileChildrenAtExpirationTime</code>&#x9636;&#x6BB5;&#x4E86;</p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">finishClassComponent</span>(<span class="hljs-params">
  current: Fiber | null,
  workInProgress: Fiber,
  shouldUpdate: boolean,
  hasContext: boolean,
  renderExpirationTime: ExpirationTime,
</span>) </span>{
  <span class="hljs-comment">// Refs should update even if shouldComponentUpdate returns false</span>
  markRef(current, workInProgress)

  <span class="hljs-keyword">const</span> didCaptureError = (workInProgress.effectTag &amp; DidCapture) !== NoEffect

  <span class="hljs-keyword">if</span> (!shouldUpdate &amp;&amp; !didCaptureError) {
    <span class="hljs-comment">// Context providers should defer to sCU for rendering</span>
    <span class="hljs-keyword">if</span> (hasContext) {
      invalidateContextProvider(workInProgress, <span class="hljs-literal">false</span>)
    }

    <span class="hljs-keyword">return</span> bailoutOnAlreadyFinishedWork(current, workInProgress)
  }

  <span class="hljs-keyword">const</span> ctor = workInProgress.type
  <span class="hljs-keyword">const</span> instance = workInProgress.stateNode

  <span class="hljs-comment">// Rerender</span>
  ReactCurrentOwner.current = workInProgress
  <span class="hljs-keyword">let</span> nextChildren
  <span class="hljs-keyword">if</span> (
    didCaptureError &amp;&amp;
    (!enableGetDerivedStateFromCatch ||
      <span class="hljs-keyword">typeof</span> ctor.getDerivedStateFromCatch !== <span class="hljs-string">&apos;function&apos;</span>)
  ) {
    nextChildren = <span class="hljs-literal">null</span>

    <span class="hljs-keyword">if</span> (enableProfilerTimer) {
      stopBaseRenderTimerIfRunning()
    }
  } <span class="hljs-keyword">else</span> {
    nextChildren = instance.render()
  }

  <span class="hljs-comment">// React DevTools reads this flag.</span>
  workInProgress.effectTag |= PerformedWork
  <span class="hljs-keyword">if</span> (didCaptureError) {
    reconcileChildrenAtExpirationTime(
      current,
      workInProgress,
      <span class="hljs-literal">null</span>,
      renderExpirationTime,
    )
    workInProgress.child = <span class="hljs-literal">null</span>
  }
  reconcileChildrenAtExpirationTime(
    current,
    workInProgress,
    nextChildren,
    renderExpirationTime,
  )
  <span class="hljs-comment">// Memoize props and state using the values we just used to render.</span>
  <span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Restructure so we never read values from the instance.</span>
  memoizeState(workInProgress, instance.state)
  memoizeProps(workInProgress, instance.props)

  <span class="hljs-comment">// The context might have changed so we need to recalculate it.</span>
  <span class="hljs-keyword">if</span> (hasContext) {
    invalidateContextProvider(workInProgress, <span class="hljs-literal">true</span>)
  }

  <span class="hljs-keyword">return</span> workInProgress.child
}</code></pre>
</div></div></div>



<div class="api-method"><div class="api-method-definition"><h1 id="updateClassInstance">updateClassInstance</h1><p>&#x8FD9;&#x4E2A;&#x4EE3;&#x7801;&#x5C31;&#x662F;&#x6309;&#x7167;&#x6D41;&#x7A0B;&#x6765;&#x529E;&#x4E8B;&#x6765;&#xFF0C;&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#x5C31;&#x4E0D;&#x8BE6;&#x7EC6;&#x8BB2;&#x4E86;&#x3002;&#x4E3B;&#x8981;&#x5C31;&#x662F;&#x8C03;&#x7528;&#x6765;&#x66F4;&#x65B0;&#x76F8;&#x5173;&#x7684;&#x51E0;&#x4E2A;&#x751F;&#x547D;&#x5468;&#x671F;&#x65B9;&#x6CD5;</p><p><code>checkShouldComponentUpdate</code>&#x91CC;&#x9762;&#x4F1A;&#x8C03;&#x7528;<code>shouldComponentUpdate</code>&#x751F;&#x547D;&#x5468;&#x671F;&#x65B9;&#x6CD5;</p><p><strong>&#x8FD9;&#x91CC;&#x770B;&#x51FA;&#x6765;&#x662F;&#x5426;&#x9700;&#x8981;<code>update</code>&#x53EA;&#x53D6;&#x51B3;&#x4E8E;&#x4F60;<code>shouldComponentUpdate</code>&#x7684;&#x8FD4;&#x56DE;&#x4EE5;&#x53CA;&#x662F;&#x5426;&#x662F;<code>PureComponent</code>&#x5E76;&#x4E14;<code>props</code>&#x548C;<code>state</code>&#x6CA1;&#x6709;&#x53D8;&#x5316;</strong></p><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkShouldComponentUpdate</span>(<span class="hljs-params">
  workInProgress,
  ctor,
  oldProps,
  newProps,
  oldState,
  newState,
  nextLegacyContext,
</span>) </span>{
  <span class="hljs-keyword">const</span> instance = workInProgress.stateNode
  <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> instance.shouldComponentUpdate === <span class="hljs-string">&apos;function&apos;</span>) {
    startPhaseTimer(workInProgress, <span class="hljs-string">&apos;shouldComponentUpdate&apos;</span>)
    <span class="hljs-keyword">const</span> shouldUpdate = instance.shouldComponentUpdate(
      newProps,
      newState,
      nextLegacyContext,
    )
    stopPhaseTimer()

    <span class="hljs-keyword">if</span> (__DEV__) {
      warningWithoutStack(
        shouldUpdate !== <span class="hljs-literal">undefined</span>,
        <span class="hljs-string">&apos;%s.shouldComponentUpdate(): Returned undefined instead of a &apos;</span> +
          <span class="hljs-string">&apos;boolean value. Make sure to return true or false.&apos;</span>,
        getComponentName(ctor) || <span class="hljs-string">&apos;Component&apos;</span>,
      )
    }

    <span class="hljs-keyword">return</span> shouldUpdate
  }

  <span class="hljs-keyword">if</span> (ctor.prototype &amp;&amp; ctor.prototype.isPureReactComponent) {
    <span class="hljs-keyword">return</span> (
      !shallowEqual(oldProps, newProps) || !shallowEqual(oldState, newState)
    )
  }

  <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
}</code></pre>
</div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updateClassInstance</span>(<span class="hljs-params">
  current: Fiber,
  workInProgress: Fiber,
  renderExpirationTime: ExpirationTime,
</span>): <span class="hljs-title">boolean</span> </span>{
  <span class="hljs-keyword">const</span> ctor = workInProgress.type
  <span class="hljs-keyword">const</span> instance = workInProgress.stateNode

  <span class="hljs-keyword">const</span> oldProps = workInProgress.memoizedProps
  <span class="hljs-keyword">const</span> newProps = workInProgress.pendingProps
  instance.props = oldProps

  <span class="hljs-keyword">const</span> oldContext = instance.context
  <span class="hljs-keyword">const</span> newUnmaskedContext = getUnmaskedContext(workInProgress)
  <span class="hljs-keyword">const</span> newContext = getMaskedContext(workInProgress, newUnmaskedContext)

  <span class="hljs-keyword">const</span> getDerivedStateFromProps = ctor.getDerivedStateFromProps
  <span class="hljs-keyword">const</span> hasNewLifecycles =
    <span class="hljs-keyword">typeof</span> getDerivedStateFromProps === <span class="hljs-string">&apos;function&apos;</span> ||
    <span class="hljs-keyword">typeof</span> instance.getSnapshotBeforeUpdate === <span class="hljs-string">&apos;function&apos;</span>

  <span class="hljs-keyword">if</span> (
    !hasNewLifecycles &amp;&amp;
    (<span class="hljs-keyword">typeof</span> instance.UNSAFE_componentWillReceiveProps === <span class="hljs-string">&apos;function&apos;</span> ||
      <span class="hljs-keyword">typeof</span> instance.componentWillReceiveProps === <span class="hljs-string">&apos;function&apos;</span>)
  ) {
    <span class="hljs-keyword">if</span> (oldProps !== newProps || oldContext !== newContext) {
      callComponentWillReceiveProps(
        workInProgress,
        instance,
        newProps,
        newContext,
      )
    }
  }

  resetHasForceUpdateBeforeProcessing()

  <span class="hljs-keyword">const</span> oldState = workInProgress.memoizedState
  <span class="hljs-keyword">let</span> newState = (instance.state = oldState)
  <span class="hljs-keyword">let</span> updateQueue = workInProgress.updateQueue
  <span class="hljs-keyword">if</span> (updateQueue !== <span class="hljs-literal">null</span>) {
    processUpdateQueue(
      workInProgress,
      updateQueue,
      newProps,
      instance,
      renderExpirationTime,
    )
    newState = workInProgress.memoizedState
  }

  <span class="hljs-keyword">if</span> (
    oldProps === newProps &amp;&amp;
    oldState === newState &amp;&amp;
    !hasContextChanged() &amp;&amp;
    !checkHasForceUpdateAfterProcessing()
  ) {
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> instance.componentDidUpdate === <span class="hljs-string">&apos;function&apos;</span>) {
      <span class="hljs-keyword">if</span> (
        oldProps !== current.memoizedProps ||
        oldState !== current.memoizedState
      ) {
        workInProgress.effectTag |= Update
      }
    }
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> instance.getSnapshotBeforeUpdate === <span class="hljs-string">&apos;function&apos;</span>) {
      <span class="hljs-keyword">if</span> (
        oldProps !== current.memoizedProps ||
        oldState !== current.memoizedState
      ) {
        workInProgress.effectTag |= Snapshot
      }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
  }

  <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> getDerivedStateFromProps === <span class="hljs-string">&apos;function&apos;</span>) {
    applyDerivedStateFromProps(
      workInProgress,
      getDerivedStateFromProps,
      newProps,
    )
    newState = workInProgress.memoizedState
  }

  <span class="hljs-keyword">const</span> shouldUpdate =
    checkHasForceUpdateAfterProcessing() ||
    checkShouldComponentUpdate(
      workInProgress,
      oldProps,
      newProps,
      oldState,
      newState,
      newContext,
    )

  <span class="hljs-keyword">if</span> (shouldUpdate) {
    <span class="hljs-comment">// In order to support react-lifecycles-compat polyfilled components,</span>
    <span class="hljs-comment">// Unsafe lifecycles should not be invoked for components using the new APIs.</span>
    <span class="hljs-keyword">if</span> (
      !hasNewLifecycles &amp;&amp;
      (<span class="hljs-keyword">typeof</span> instance.UNSAFE_componentWillUpdate === <span class="hljs-string">&apos;function&apos;</span> ||
        <span class="hljs-keyword">typeof</span> instance.componentWillUpdate === <span class="hljs-string">&apos;function&apos;</span>)
    ) {
      startPhaseTimer(workInProgress, <span class="hljs-string">&apos;componentWillUpdate&apos;</span>)
      <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> instance.componentWillUpdate === <span class="hljs-string">&apos;function&apos;</span>) {
        instance.componentWillUpdate(newProps, newState, newContext)
      }
      <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> instance.UNSAFE_componentWillUpdate === <span class="hljs-string">&apos;function&apos;</span>) {
        instance.UNSAFE_componentWillUpdate(newProps, newState, newContext)
      }
      stopPhaseTimer()
    }
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> instance.componentDidUpdate === <span class="hljs-string">&apos;function&apos;</span>) {
      workInProgress.effectTag |= Update
    }
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> instance.getSnapshotBeforeUpdate === <span class="hljs-string">&apos;function&apos;</span>) {
      workInProgress.effectTag |= Snapshot
    }
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// If an update was already in progress, we should schedule an Update</span>
    <span class="hljs-comment">// effect even though we&apos;re bailing out, so that cWU/cDU are called.</span>
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> instance.componentDidUpdate === <span class="hljs-string">&apos;function&apos;</span>) {
      <span class="hljs-keyword">if</span> (
        oldProps !== current.memoizedProps ||
        oldState !== current.memoizedState
      ) {
        workInProgress.effectTag |= Update
      }
    }
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> instance.getSnapshotBeforeUpdate === <span class="hljs-string">&apos;function&apos;</span>) {
      <span class="hljs-keyword">if</span> (
        oldProps !== current.memoizedProps ||
        oldState !== current.memoizedState
      ) {
        workInProgress.effectTag |= Snapshot
      }
    }

    <span class="hljs-comment">// If shouldComponentUpdate returned false, we should still update the</span>
    <span class="hljs-comment">// memoized props/state to indicate that this work can be reused.</span>
    workInProgress.memoizedProps = newProps
    workInProgress.memoizedState = newState
  }

  <span class="hljs-comment">// Update the existing instance&apos;s state, props, and context pointers even</span>
  <span class="hljs-comment">// if shouldComponentUpdate returns false.</span>
  instance.props = newProps
  instance.state = newState
  instance.context = newContext

  <span class="hljs-keyword">return</span> shouldUpdate
}</code></pre>
</div></div></div>




                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
<div id="disqus_thread"></div>
<script>
  /**
   *  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
   *  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
  /*
var disqus_config = function () {
this.page.url = PAGE_URL;  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
  ;(function() {
    // DON'T EDIT BELOW THIS LINE
    var d = document,
      s = d.createElement('script')
    s.src = 'https://react-source-code.disqus.com/embed.js'
    s.setAttribute('data-timestamp', +new Date())
    ;(d.head || d.body).appendChild(s)
  })()
</script>
<noscript
  >Please enable JavaScript to view the
  <a href="https://disqus.com/?ref_noscript"
    >comments powered by Disqus.</a
  ></noscript
>

            </div>

            

        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"updateClassComponent","level":"1.4.7.4","depth":3,"next":{"title":"updateHostRoot","level":"1.4.7.5","depth":3,"path":"book/flow/begin-work/update-host-root.md","ref":"book/flow/begin-work/update-host-root.md","articles":[]},"previous":{"title":"updateFunctionalComponent","level":"1.4.7.3","depth":3,"path":"book/flow/begin-work/update-functional-component.md","ref":"book/flow/begin-work/update-functional-component.md","articles":[]},"dir":"ltr"},"config":{"plugins":["theme-api"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"sharing":{"facebook":false,"twitter":false,"google":false,"weibo":true,"instapaper":false,"vk":false,"all":["weibo","google","twitter","weibo","instapaper"]},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-api":{"languages":[],"split":true,"theme":"light"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"React源码解析","language":"zh-hans","gitbook":"*"},"file":{"path":"book/flow/begin-work/update-class-component.md","mtime":"2018-11-19T08:19:37.703Z","type":"markdown"},"gitbook":{"version":"3.2.0","time":"2019-07-08T12:28:32.264Z"},"basePath":"../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../gitbook/gitbook.js"></script>
    <script src="../../../gitbook/theme.js"></script>
    
        
        <script src="../../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-theme-api/theme-api.js"></script>
        
    

    </body>
</html>

